package ru.susu.algebra.partition.writer;

import java.io.IOException;
import java.io.Writer;
import java.util.Collection;

import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.util.NumberUtilities;
import ru.susu.algebra.util.StringUtilities;

/**
 * @author akargapolov
 * @since: 09.03.2009
 */
public class FormattedPartitionsWriter implements IPartitionsWriter
{

	public boolean _withNumber = true;

	@Override
	public void write(Collection<Partition> partitions, Writer out) throws IOException
	{
		int index = 0;
		int maxNumber = 0;
		for (Partition partition:partitions)
		{
			maxNumber = Math.max(maxNumber, partition.getOrderNumber());
		}
		int width = NumberUtilities.getLength(maxNumber, 10) + 1;
		int width1 = NumberUtilities.getLength(partitions.size(), 10);
		for (Partition partition : partitions)
		{
			if (_withNumber)
				out.write(StringUtilities.toString(index++, width1) + ":");
			partition.writeTo(out, width);
			out.write("\n");
		}
		out.flush();
	}

}
